/**
 * author hary hary@taomee.com
 */

(function($){
	$.fn.shake = function(options){
			
		    direction = ['horizontal','vertical'];
		    
			defaultOptions = {
					locus:new Array(15, 30, 15, 0, -15, -30, -15, 0),
					time :3,
					direct: direction[0],
					rate:20
			}
			
			options = $.extend(defaultOptions,options||{});
			
			function loadEvent(obj)
			{
				var locus = [];

				$(obj).css('position','relative');

				for(var i = 0;i < options.time;i++)
				{
					locus = locus.concat(options.locus);
				}

				shake($(obj),locus,options.rate,options.direct);
			}
			
			function shake(obj,locus,rate,direct)
			{
				if($.isArray(locus))
				{
					first = locus.shift();
				}
				
				changeStyle(obj, first,direct);
				
				if (locus.length > 0) 
				{
					setTimeout(function() {
						shake(obj, locus, rate,direct);
					}, rate);
				} 
				else 
				{
					try {
						obj.css('position','static');
					} catch (e) {}
				}
			}
			
			function changeStyle(obj,position,direct)
			{
				if(direct == direction[0])
				{
					obj.css('left',position+"px");
				}
				else if(direct == direction[1])
				{
					obj.css('top',position+"px");
				}
			}
			
			this.each(function(){
				loadEvent(this);
			});
			
	}
})(jQuery)